package cn.tianshi.board;

import cn.tianshi.board.bean.ApiModel;
import cn.tianshi.board.bean.Body;
import cn.tianshi.board.enums.TypeEnums;
import com.google.common.base.Strings;
import org.apache.commons.lang.StringUtils;

import java.util.ArrayList;

/**
 * @author lin2.zhang
 */
public class Analysis {

    public ApiModel analysis(Integer routerId, String excel) {
        Integer mappingId = routerId*100;
        String[] splits = excel.replaceAll("\\r\\n", "\\n").split("\\n");
        TypeEnums type = TypeEnums.NONE;
        ApiModel model = new ApiModel();
        model.setRequestBody(new ArrayList<>());
        model.setResponseBody(new ArrayList<>());
        model.setRouterId(routerId.toString());
        model.setMappingId(--mappingId);
        for (String split : splits) {
            split = split.replaceAll(" ","").replaceAll("'","\\\\'");
            type = analysisLine(split, model, type);
        }
        return model;
    }

    public TypeEnums analysisLine(String line, ApiModel model, TypeEnums type) {
        if (Strings.isNullOrEmpty(line)) {
            return type;
        }
        String trim = line.replaceAll("\\t", "").trim();
        if (trim.startsWith("接口名:")) {
            type = TypeEnums.HEAD;
        }
        if (trim.startsWith("请求报文体")) {
            type = TypeEnums.REQUEST;
        }
        if (trim.startsWith("响应报文体")) {
            type = TypeEnums.RESPONSE;
        }
        switch (type) {
            case HEAD: head(line, model); break;
            case REQUEST: request(line, model); break;
            case RESPONSE: response(line, model); break;
            default:
        }
        return type;
    }

    public void head(String line, ApiModel model) {
        String[] splits = line.split("\\t");
        int type = 0;
        for (String split : splits) {
            String trim = split.trim();
            if (trim.startsWith("接口名")) {
                type = 1;
                continue;
            }
            if (trim.startsWith("swagger接口大类")) {
                type = 2;
                continue;
            }
            if (trim.startsWith("地址")) {
                type = 3;
                continue;
            }
            if (trim.startsWith("金融系统地址")) {
                type = 4;
                continue;
            }
            if (trim.startsWith("是否登录")) {
                type = 5;
                continue;
            }
            if (trim.startsWith("转换方式")) {
                type = 6;
                continue;
            }
            if (trim.startsWith("金融系统请求方式")) {
                type = 7;
                continue;
            }
            if (StringUtils.isNotBlank(split)) {
                if (type == 1) {
                    model.setApiName(trim);
                    type = 0;
                    continue;
                }
                if (type == 2) {
                    model.setSwaggerName(trim);
                    type = 0;
                    continue;
                }
                if (type == 3) {
                    model.setGatewayUrl(trim);
                    type = 0;
                    continue;
                }
                if (type == 4) {
                    model.setCoreUrl(trim);
                    type = 0;
                    continue;
                }
                if (type == 5) {
                    model.setLoginFlag(trim);
                    type = 0;
                    continue;
                }
                if (type == 6) {
                    model.setHandlerType(trim);
                    type = 0;
                    continue;
                }
                if (type == 7) {
                    model.setMappingMethod(trim);
                    type = 0;
                }
            }
        }
    }

    public void request(String line, ApiModel model) {
        if (line.contains("请求报文体") || line.replaceAll("\\t","").length() < 5) {
            return;
        }
        if (line.contains("rows\trows")) {
            return;
        }
        Body body = new Body();
        String[] splits = line.split("\\t");
        if (line.contains("┗")) {
            if (model.getRequestBody().get(model.getRequestBody().size() - 1).getRows() == null) {
                model.getRequestBody().get(model.getRequestBody().size() - 1).setRows(new ArrayList<>());
            }
            String pid = model.getRequestBody().get(model.getRequestBody().size() - 1).getId();
            body.setPid(pid);
            model.getRequestBody().get(model.getRequestBody().size() - 1).getRows().add(body);
        } else {
            body.setPid("0");
            model.getRequestBody().add(body);
        }
        int pointer = 0;
        boolean startFlag = false;
        for (String split : splits) {
            String trim = split.trim().replaceAll("┗", "");
            if (StringUtils.isNotBlank(trim) && !startFlag) {
                startFlag = true;
                Integer mappingId = model.getMappingId();
                mappingId++;
                model.setMappingId(mappingId);
                body.setId(mappingId.toString());
                body.setRouterId(model.getRouterId());
                body.setType("1");
            }
            if (startFlag) {
                pointer++;
            }
            //域名称
            if (pointer == 2) {
                body.setRemark(trim);
            }
            //变量名
            if (pointer == 3) {
                body.setAlias(trim);
            }
            //金融系统变量名
            if (pointer == 4) {
                body.setOuterName(trim);
            }
            //数据类型
            if (pointer == 5) {
            }
            //域说明
            if (pointer == 6) {
                body.setRemark(body.getRemark() + trim);
            }
            //是否必填
            if (pointer == 7) {
            }
        }
    }

    public void response(String line, ApiModel model) {
        if (line.contains("响应报文体") || line.replaceAll("\\t","").length() < 5) {
          return;
        }
        if (line.contains("rows\trows")) {
            return;
        }
        Body responseBody = new Body();
        String[] splits = line.split("\\t");
        if (line.contains("┗")) {
            if (model.getResponseBody().get(model.getResponseBody().size() - 1).getRows() == null) {
                model.getResponseBody().get(model.getResponseBody().size() - 1).setRows(new ArrayList<>());
            }
            String pid = model.getResponseBody().get(model.getResponseBody().size() - 1).getId();
            responseBody.setPid(pid);
            model.getResponseBody().get(model.getResponseBody().size() - 1).getRows().add(responseBody);
        } else {
            responseBody.setPid("0");
            model.getResponseBody().add(responseBody);
        }
        int pointer = 0;
        boolean startFlag = false;
        for (String split : splits) {
            String trim = split.trim().replaceAll("┗", "");
            if (StringUtils.isNotBlank(trim) && !startFlag) {
                startFlag = true;
                Integer mappingId = model.getMappingId();
                mappingId++;
                model.setMappingId(mappingId);
                responseBody.setId(mappingId.toString());
                responseBody.setRouterId(model.getRouterId());
                responseBody.setType("2");
            }
            if (startFlag) {
                pointer++;
            }
            //域名称
            if (pointer == 2) {
                responseBody.setRemark(trim);
            }
            //变量名
            if (pointer == 3) {
                responseBody.setAlias(trim);
            }
            //金融系统变量名
            if (pointer == 4) {
                responseBody.setOuterName(trim);
            }
            //数据类型
            if (pointer == 5) {
            }
            //域说明
            if (pointer == 6) {
                responseBody.setRemark(responseBody.getRemark() + trim);
            }
            //是否必填
            if (pointer == 7) {
            }
        }
    }
}
